package com.hgz.sbj.controller;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.hgz.sbj.model.TFxRecord;
import com.hgz.sbj.service.TFxRecordService;

@Controller
@RequestMapping("/record")
public class RecordController {
	
	private Logger logger = LoggerFactory.getLogger(RecordController.class);

	@Autowired
	private TFxRecordService tFxRecordService;

	@RequestMapping("/records")
	public String showRecords(Model model) {
		try {
			List<TFxRecord> records = tFxRecordService.getAllTFxRecords();
			model.addAttribute("list", records);
			logger.info(records.toString());
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("获取记录列表异常！");
		}
		return "record-list";
	}
	
	/*public String showRecordInfo(Model model){
		TFxText text = tFxRecordService.getText(fxId);
		model.addAttribute("", arg1);
		return "recordinfo-modal";
	}*/
	
	@RequestMapping(value="/upload",method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> uploadFile(HttpServletRequest request) {
		MultipartHttpServletRequest fileRequest = (MultipartHttpServletRequest) request;
		MultipartFile file = fileRequest.getFile("myfile");
		String rootPath = request.getSession().getServletContext().getRealPath(File.separator);
		tFxRecordService.readExcel(file, rootPath, 1, 1);
		Map<String, Object> dataMap = new HashMap<String, Object>();
	    dataMap.put("result", "success");
	    return dataMap;
	}
}
